<!--
 * @Author: LiYuan
 * @Date: 2021-03-08 19:27:37
 * @LastEditTime: 2022-08-15 10:47:42
 * @LastEditors: huangbc
 * @Description: 事件转交
 * You build it, You run it.
-->
<template>
  <base-modal
    :title="$t('event.components.eventBatchReAllotDialog.text5')"
    :show.sync="visible"
    width="600px"
    class="batch-event-reallot-dialog"
  >
  
    <div class="dialog-body">
      <div class="dialog-body-content">
        {{$t('event.components.eventBatchReAllotDialog.text1')}}
        <el-tooltip
          :content="$t('event.components.eventBatchReAllotDialog.text6')"
          placement="top"
        >
        <i class="iconfont icon-question task-icon"></i> </el-tooltip
        ><br />
        {{$t('event.components.eventBatchReAllotDialog.text2')}}
        <p @click="checkDept(0)">
          <template v-if="isOpenData && executorStaffId">
            <open-data type='userName' :openid="executorStaffId"></open-data>
          </template>
          <template v-else>
            {{ executorName || $t('event.components.eventBatchReAllotDialog.text3') }}
          </template>
        </p>
        <p @click="checkDept(1)">
          <template v-if="isOpenData && synergies && synergies.length">
            <open-data v-for="synergy in synergies" :key="synergy.staffId" type='userName' :openid="synergy.staffId"></open-data>
          </template>
          <template v-else>
            {{ synergiesNames || $t('event.components.eventBatchReAllotDialog.text4') }}
          </template>
        </p>
      </div>
    </div>

    <div slot="footer" class="dialog-footer">
      <el-button @click="visible = false">{{$t('common.base.cancel')}}</el-button>
      <el-button type="primary" @click="onSubmit" :disabled="pending"
        >{{$t('common.base.makeSure')}}</el-button
      >
    </div>
  </base-modal>
</template>

<script>
import { isOpenData } from '@src/util/platform'
import * as EventApi from "@src/api/EventApi.js";
import { BaseSelectUserModeEnum } from '@src/component/common/BaseSelectUser/model/enum'
import { chooseExUser } from '@model/config/SelectUserConfig.ts'
export default {
  name: "event-batch-reallot-dialog",
  props: {
    eventIdList: {
      type: Array, //ids
      default: [],
    },
  },
  data() {
    return {
      isOpenData,
      visible: false,
      pending: false,
      synergies: [],
      executorStaffId: '',
      executorId: '',
      synergiesNames: '', // 协同人
      executorName: '', // 负责人
    }
  },
  methods: {
    open() {
      this.visible = true
    },
    
    /**
     * @description 事件转派提交
     */
    async onSubmit() {
      const { eventIdList, synergies, executorId, executorName } = this;
      const fromId = window.frameElement.getAttribute('id');
      const params = {
        eventIdList,
        synergies,
        executorId,
        executorName
      };
      if (!executorId) {
        this.$message.warning(this.$t('event.components.eventBatchReAllotDialog.text7'));
        return;
      }
      this.pending = true
      const { status, message, data, succ } = await EventApi.eventRedeployBatch(
        params
      );
      this.pending = false
      
      if (status === 0) {
        this.$notify({
          title: this.$t('common.base.success'),
          message: this.$t('event.components.eventBatchReAllotDialog.text8'),
          type: 'success'
        });
        this.visible = false;
        this.refreshTab(fromId)
      } else if (status === 1) {
        let msg = message;
        if (data) {
          msg += '： ';
          data.fail.forEach((item, index) => {
            msg += item.eventNo;
            if (index !== data.fail.length - 1) {
              msg += ',';
            }
          });
          msg += this.$t('common.modal.BatchAllotPartSuccessMessage', {successCount: data.succ.length});
        }
        this.visible = false;
        const confirm = await this.$platform.confirm(msg, {customClass: 'event-redeploy-batch-confirm'});
        if(!confirm) return;
        this.refreshTab(fromId)
      }
    },

    // 刷新Tab
    refreshTab(fromId){
      window.__exports__refresh = '';
      this.$platform.refreshTab(fromId);
    },

    /**
     * @description 选择负责人和协同人
     */
    checkDept(type = 0) {
      // type 1协同人 0 负责人
      const name = !type ? "team" : "dept";
      let option = {
        ...(type ? {} : {max:1}),
        ...chooseExUser,
        mode: BaseSelectUserModeEnum.Filter
      };
      this.$fast.select.multi.all(option).then(({ data, status }) => {
        
        if (!status) {
          const { userId, displayName, staffId } = data.users[0];
          if (type) {
            this.synergies = data.users;
            this.synergiesNames = data.users
              .map((item) => {
                return item.displayName;
              })
              .join(',');
          } else {
            this.executorStaffId = staffId;
            this.executorId = userId;
            this.executorName = displayName;
          }
        }
        
      });
    },
  }
}
</script>

<style lang="scss" scoped>
.batch-event-reallot-dialog {
  padding: 0 10px;
  .dialog-body {
    padding: 15px;
    padding-bottom: 30px;
    margin: 10px auto;
    width: 96%;
    &-content {
      font-size: 12px;
      background-color: #d9edf7;
      padding: 10px 25px;
      > p {
        min-height: 30px;
        color: #999;
        line-height: 30px;
        border: 1px solid #e0e1e2;
        background-color: #fff;
        padding-left: 10px;
        margin-top: 10px;
        margin-bottom: 0;
      }
    }
  }
  .dialog-footer {
    display: flex;
    justify-content: flex-end;
  }
}
</style>
<style lang="scss">
.event-redeploy-batch-confirm {
  .el-message-box__content {
    word-break: break-all !important;
  }
}
</style>